"""
服务数据映射层
"""


class ServiceMapper:
    """服务数据映射 - 调用模型的转换方法"""
    
    def to_dict(self, service, include_relations=True):
        """将Service模型转换为字典"""
        if not service:
            return None
        return service.to_dict(include_relations=include_relations)
    
    def to_list(self, services, include_relations=True):
        """将Service列表转换为字典列表"""
        return [s.to_dict(include_relations=include_relations) for s in services]
    
    def from_dict(self, data):
        """从字典提取Service数据（用于更新）"""
        # 返回可用于更新模型的字典
        update_data = {}
        if 'name' in data:
            update_data['name'] = data['name']
        if 'price' in data:
            update_data['price'] = data.get('price', 0)
        if 'description' in data:
            update_data['description'] = data.get('description')
        if 'typeId' in data:
            update_data['type_id'] = data.get('typeId')
        return update_data


class ServiceTypeMapper:
    """服务类型数据映射 - 调用模型的转换方法"""
    
    def to_dict(self, service_type):
        """将ServiceType模型转换为字典"""
        if not service_type:
            return None
        return service_type.to_dict()
    
    def to_list(self, service_types):
        """将ServiceType列表转换为字典列表"""
        return [st.to_dict() for st in service_types]
